/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#include "phys_local.h"

DLL_EXPORT IPhysics *CreatePhysics	( void ) {	return new EPhysics(); }

/*-----------------------------------------------------------------------------
	Physics local stuff :
-----------------------------------------------------------------------------*/

//
//	EPhysics::EPhysics
//
EPhysics::EPhysics( void )
{
	LOG_SPLIT("Physics initialization");

	// Build the broadphase
	broadphase				= new btDbvtBroadphase();

	// Set up the collision configuration and dispatcher
	collisionConfiguration	= new btDefaultCollisionConfiguration();
	dispatcher				= new btCollisionDispatcher( collisionConfiguration );

	// The actual physics solver
	solver					= new btSequentialImpulseConstraintSolver();

	// The world
	dynamicsWorld			= new btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration );
	
	EPhysCharacter::InitGhostCallback( broadphase );
	

	//btVector3 worldMin(-1000,-1000,-1000);
	//btVector3 worldMax(1000,1000,1000);
	//broadphase = new btAxisSweep3(worldMin,worldMax);
	//solver = new btSequentialImpulseConstraintSolver();
	//dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);

	//	Init debug rendering :
	CONFIG_REGISTER_VAR(	phys_debug					,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_wireframe			,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_aabb				,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_features_text		,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_contact_points	,	false	);
	CONFIG_REGISTER_VAR(	phys_no_deactivation		,	false	);
	CONFIG_REGISTER_VAR(	phys_no_help_text 			,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_text				,	false	);
	CONFIG_REGISTER_VAR(	phys_profile_timings 		,	false	);
	CONFIG_REGISTER_VAR(	phys_enable_sat_comparison 	,	false	);
	CONFIG_REGISTER_VAR(	phys_disable_bullet_lcp		,	false	);
	CONFIG_REGISTER_VAR(	phys_enable_ccd 			,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_constraints 		,	false	);
	CONFIG_REGISTER_VAR(	phys_draw_constraint_limits ,	false	);
	CONFIG_REGISTER_VAR(	phys_fast_wireframe 		,	false	);

	CONFIG_REGISTER_VAR(	phys_max_substeps			,	10				);
	CONFIG_REGISTER_VAR(	phys_fixed_timestep			,	1.0f / 60.0f	);
	
	CONFIG_REGISTER_VAR(	phys_linear_damping			,	0.01f	);
	CONFIG_REGISTER_VAR(	phys_angular_damping		,	0.01f	);
	CONFIG_REGISTER_VAR(	phys_linear_sleep_treshold	,	0.01f	);
	CONFIG_REGISTER_VAR(	phys_angular_sleep_treshold	,	0.01f	);
	CONFIG_REGISTER_VAR(	phys_contact_proc_treshold	,	0.01f	);
	
	InitDebugRender();
	
	dynamicsWorld->setGravity( btVector3(0, 0, 0) );

	LOG_SPLIT("");

	//btActionInterface
	//btDiscreteDynamicsWorld
}


//
//	EPhysics::~EPhysics
//
EPhysics::~EPhysics( void )
{
	LOG_SPLIT("Physics shutting down");
	
	EPhysCharacter::ShutdownGhostCallback( broadphase );
	
	ShutdownDebugRender();
	
	delete dynamicsWorld;			   dynamicsWorld			=	NULL;
	delete solver;					   solver					=	NULL;
	delete dispatcher;				   dispatcher				=	NULL;
	delete collisionConfiguration;	   collisionConfiguration	=	NULL;
	delete broadphase;				   broadphase				=	NULL;

	LOG_SPLIT("");
}


//
//	EPhysics::RunSimulation
//
void EPhysics::RunSimulation( uint dtime )
{	
	int		max_substeps	=		phys_max_substeps->Int();
	float	fixed_timestep	=		phys_fixed_timestep->Float();
	dynamicsWorld->stepSimulation( dtime/1000.0f, max_substeps, fixed_timestep );
}


//
//	EPhysics::SetGravity
//
void EPhysics::SetGravity( const EVector &gravity )
{
	dynamicsWorld->setGravity( ToVector( gravity ) );
}


//
//	EPhysics::AddEntity
//
IPxPhysEntity EPhysics::AddEntity( void )
{
	return new EPhysEntity(this);
}


//
//	EPhysics::AddCharacter
//
IHrPhysCharacter EPhysics::AddCharacter( float height, float radius, float climbing_slope, float stair_step )
{
	return new EPhysCharacter( this, height, radius, climbing_slope, stair_step );
}


//
//	EPhysics::Add6DOF
//
IPxPhys6DOF EPhysics::Add6DOF( IPxPhysEntity a, IPxPhysEntity b, EMatrix &basis_a, const EMatrix &basis_b )
{
	return new EPhys6DOF( this, a, b, basis_a, basis_b );
}


//
//	EPhysics::Add6DOF
//
IPxPhys6DOF EPhysics::Add6DOF( IPxPhysEntity b, const EMatrix &basis_b )
{
	return new EPhys6DOF( this, b, basis_b );
}


//
//	EPhysics::GetGravity
//
EVector EPhysics::GetGravity( void ) const
{
	return ToVector( dynamicsWorld->getGravity() );
}

